﻿Imports System.Data.SqlClient

Public Class FrmProveedores

    Dim controller As New Class_RC_Controller.rc_Proveedores

    Private _codProveedor As Integer = 0

    Public Property codProveedor() As Integer
        Get
            Return _codProveedor
        End Get
        Set(value As Integer)
            _codProveedor = value
            txtcodproveedor.Text = value.ToString().PadLeft(4, "0")
        End Set
    End Property

    Public Sub inhabilitar()
        'inhabilitar los textbox
        Me.txtcodproveedor.Enabled = False
        Me.txtnombre.Enabled = False
        Me.txtdireccion.Enabled = False
        Me.txttelefono.Enabled = False
        Me.txtcelular.Enabled = False
        Me.cmbciudad.Enabled = False
        Me.txtruc.Enabled = False
        Me.txtcodproveedor.BackColor = System.Drawing.Color.White
        Me.txtnombre.BackColor = System.Drawing.Color.White
        Me.txtdireccion.BackColor = System.Drawing.Color.White
        Me.txttelefono.BackColor = System.Drawing.Color.White
        Me.txtcelular.BackColor = System.Drawing.Color.White
        Me.cmbciudad.BackColor = System.Drawing.Color.White
        Me.txtruc.BackColor = System.Drawing.Color.White

        'inhabilitar los botones
        Me.btnguardar.Enabled = False

        Me.btneliminar.Enabled = True

        Me.btncancelar.Enabled = False

    End Sub

    Public Sub habilitar()
        'habilitar controles para permitir la insersion
        Me.txtnombre.Enabled = True
        Me.txtdireccion.Enabled = True
        Me.txttelefono.Enabled = True
        Me.txtcelular.Enabled = True
        Me.txtruc.Enabled = True
        ' Me.txtsaldo.Enabled = True
        Me.cmbciudad.Enabled = True
        'habilitar controles
        Me.btnguardar.Enabled = True

        Me.btneliminar.Enabled = False
        Me.btncancelar.Enabled = True

        'Me.btnbuscar.Enabled = False
    End Sub

    Public Sub limpiar()
        Me.txtcodproveedor.Text = ""
        Me.txtnombre.Text = ""
        Me.txtdireccion.Text = ""
        Me.txttelefono.Text = ""
        Me.txtcelular.Text = ""
        Me.txtruc.Text = ""
        Me.cmbciudad.Text = ""

    End Sub

    Private Sub llenarCombos()

        Dim dsCiudad As DataTable
        dsCiudad = controller.get_Ciudades(oiml.dbConn)
        With Me.cmbciudad
            .DataSource = dsCiudad
            .DisplayMember = "descripcion"
            .ValueMember = "codCiudad"
        End With

    End Sub

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Private Shadows Sub KeyUpmask(ByVal MaskedTextboxs As System.Windows.Forms.MaskedTextBox, ByVal e As System.Windows.Forms.KeyEventArgs)

        'Teclas que Utilizaremos El cual solo son las numericas
        If e.KeyData = Keys.Space Or e.KeyData = Keys.Decimal Or _
         e.KeyData = Keys.NumPad0 Or e.KeyData = Keys.NumPad1 Or e.KeyData = Keys.NumPad2 Or e.KeyData = Keys.NumPad3 _
           Or e.KeyData = Keys.NumPad4 Or e.KeyData = Keys.NumPad5 Or e.KeyData = Keys.NumPad6 Or _
           e.KeyData = Keys.NumPad7 Or e.KeyData = Keys.NumPad8 Or e.KeyData = Keys.NumPad9 _
           Or e.KeyData = Keys.D0 Or e.KeyData = Keys.D1 Or e.KeyData = Keys.D2 Or e.KeyData = Keys.D3 Or _
           e.KeyData = Keys.D4 Or e.KeyData = Keys.D5 Or e.KeyData = Keys.D6 _
           Or e.KeyData = Keys.D7 Or e.KeyData = Keys.D8 Or e.KeyData = Keys.D9 Or e.KeyData = Keys.Tab Or e.KeyData = Keys.Enter _
            Then

            'Programamos la tecla de Retroceso en el textbox
        ElseIf e.KeyData = Keys.Back And MaskedTextboxs.Text.Length > 0 Then
            On Error Resume Next
            'Removemos un caracter mediante se presione la tecla Back
            MaskedTextboxs.Text.Remove(MaskedTextboxs.SelectionStart - 1)

            'si el textbox es menor que un caracter
        ElseIf MaskedTextboxs.Text.Length < 1 Then
            'ponemos el textbox En Blanco
            MaskedTextboxs.Text = String.Empty
            'ponemos la variable    NumerosYaescritos en Blanco

        Else
            'ponemos el textbox enblanco
            MaskedTextboxs.Text = String.Empty
            'Hacemos Sonar un Beep para indicarle al usuario que ha presionado un caracter y no un Numero
            Beep()
            MsgBox("Introducir solo valores numericos")
        End If

    End Sub

    Private Sub txttelefono_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txttelefono.KeyUp
        'Validar que solo se ingresen valores numericos
        Me.KeyUpmask(Me.txttelefono, e)

    End Sub

    Private Sub txtcelular_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtcelular.KeyUp
        'Validar que solo se ingresen valores numericos
        Me.KeyUpmask(Me.txtcelular, e)
    End Sub

    Private Shadows Sub KeyUp(ByVal TextBoxs As System.Windows.Forms.TextBox, ByVal e As System.Windows.Forms.KeyEventArgs)

        'Teclas que Utilizaremos El cual solo son las numericas
        If e.KeyData = Keys.Space Or e.KeyData = Keys.Decimal Or _
         e.KeyData = Keys.NumPad0 Or e.KeyData = Keys.NumPad1 Or e.KeyData = Keys.NumPad2 Or e.KeyData = Keys.NumPad3 _
           Or e.KeyData = Keys.NumPad4 Or e.KeyData = Keys.NumPad5 Or e.KeyData = Keys.NumPad6 Or _
           e.KeyData = Keys.NumPad7 Or e.KeyData = Keys.NumPad8 Or e.KeyData = Keys.NumPad9 _
           Or e.KeyData = Keys.D0 Or e.KeyData = Keys.D1 Or e.KeyData = Keys.D2 Or e.KeyData = Keys.D3 Or _
           e.KeyData = Keys.D4 Or e.KeyData = Keys.D5 Or e.KeyData = Keys.D6 _
           Or e.KeyData = Keys.D7 Or e.KeyData = Keys.D8 Or e.KeyData = Keys.D9 Or e.KeyData = Keys.Tab Or e.KeyData = Keys.Enter _
           Or e.KeyData = Keys.OemMinus Then

            'Programamos la tecla de Retroceso en el textbox
        ElseIf e.KeyData = Keys.Back And TextBoxs.Text.Length > 0 Then
            On Error Resume Next
            'Removemos un caracter mediante se presione la tecla Back
            TextBoxs.Text.Remove(TextBoxs.SelectionStart - 1)

            'si el textbox es menor que un caracter
        ElseIf TextBoxs.Text.Length < 1 Then
            'ponemos el textbox En Blanco
            TextBoxs.Text = String.Empty
            'ponemos la variable    NumerosYaescritos en Blanco

        Else
            'ponemos el textbox enblanco
            TextBoxs.Text = String.Empty
            'Hacemos Sonar un Beep para indicarle al usuario que ha presionado un caracter y no un Numero
            Beep()
            MsgBox("Introducir solo valores numericos")
        End If

    End Sub

    'Private Sub txtruc_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtruc.KeyUp
    '    'Validar que solo se ingresen valores numericos
    '    Me.KeyUp(Me.txtruc, e)
    'End Sub

    Private Sub CargarProveedor()
        Try
            If codProveedor > 0 Then

                Dim ds As New DataTable
                ds = controller.get_Proveedores_All(oiml.dbConn, codProveedor, "", "", 0)

                If Not ds Is Nothing Then

                    ' cargar los controles del formulario con los valores de los campos del registro
                    'Me.txtcodproveedor.Text = ds.Rows(0)("codProveedor").ToString
                    codProveedor = ds.Rows(0)("codProveedor").ToString
                    Me.txtnombre.Text = ds.Rows(0)("nombre").ToString
                    Me.txtdireccion.Text = ds.Rows(0)("direccion").ToString
                    Me.txttelefono.Text = ds.Rows(0)("telefono").ToString
                    Me.txtcelular.Text = ds.Rows(0)("celular").ToString
                    Me.txtruc.Text = ds.Rows(0)("ruc").ToString


                    'FormatNumber(oDataRow("saldo"), 0, , , TriState.True)

                    Me.cmbciudad.SelectedValue = ds.Rows(0)("codCiudad").ToString
                    lblUsuario.Text = ds.Rows(0)("usuario").ToString
                    lblfechaActualizacion.Text = ds.Rows(0)("fechaActualizacion_").ToString

                End If

            Else

                Me.txtcodproveedor.Text = ""
                Me.txtnombre.Text = ""
                Me.txtdireccion.Text = ""
                Me.txttelefono.Text = ""
                Me.txtcelular.Text = ""
                Me.txtruc.Text = ""

                lblUsuario.Text = oiml.Usuario
                lblfechaActualizacion.Text = Date.Now.ToShortDateString

                txtnombre.Focus()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub FrmProveedores_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        llenarCombos()
        CargarProveedor()

    End Sub

    Private Sub btneliminar_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneliminar.Click
        Try
            If codProveedor > 0 Then
                If MessageBox.Show("Desea eliminar el registro seleccionado?", "Proveedores", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                    If controller.Delete_Proveedores(oiml.dbConn, codProveedor) Then
                        Me.Close()

                    Else
                        MessageBox.Show("Error: " & controller.MessageError.Message, "Proveedores", MessageBoxButtons.OK, MessageBoxIcon.Error)

                    End If

                End If
            End If


        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub


    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtnombre.Text = "" Then
            ErrorProvider.SetError(txtnombre, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtnombre, "")
        End If

        If txtdireccion.Text = "" Then
            ErrorProvider.SetError(txtdireccion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtdireccion, "")
        End If

        If txtruc.Text = "" Then
            ErrorProvider.SetError(txtruc, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtruc, "")
        End If
        '
        Return controlCampos

    End Function

    Private Sub btnguardar_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguardar.Click
        If ValidarCampos() Then
            txtnombre.Focus()
            Exit Sub
        End If

        If codProveedor = 0 Then

            codProveedor = controller.Insert_Proveedores(oiml.dbConn, txtnombre.Text, txtdireccion.Text, txttelefono.Text, txtcelular.Text, cmbciudad.SelectedValue, txtruc.Text, oiml.idUsuario)

            If codProveedor > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Proveedores", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Proveedores", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If controller.Update_Proveedores(oiml.dbConn, codProveedor, txtnombre.Text, txtdireccion.Text, txttelefono.Text, txtcelular.Text, cmbciudad.SelectedValue, txtruc.Text, oiml.idUsuario) Then

                MessageBox.Show("Registro grabado con exito!", "Proveedores", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Proveedores", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub

    Private Sub btncancelar_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancelar.Click
        Me.Close()
    End Sub


   
   
    Private Sub lblCodigo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblCodigo.Click

    End Sub
End Class